// subpkg/recommend/recommend.js
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:true,
    input:'',
    value: 0,
    display:'none',
    fileList: [
    ],
    pictureList:[]
  },
  //更改评分
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },


  onClose(){
    this.setData({
      display:'none'
    })
  },
  addRecommend(){
    this.setData({
      display:'block'
    })
  },
  bindinput(e){
    
    this.setData({
      input:e.detail.value
    })
  },

  afterRead(event) {
    var fileList = this.data.fileList
    const { file } = event.detail;
    console.log(file)
    for(var i in file){
      fileList.push(file[i])
    }
    this.setData({
      fileList:fileList
    })
    
    
  },
  confirm(){
    if(this.data.input==''){
      Toast('请留下您宝贵的意见')
      return
    }
    else if(this.data.value==0){
      Toast('请为养老院评个分吧')
      return
    }
    this.launch()//上传图片
    if(wx.getStorageSync('identity')==='家属'){
      wx.request({
        url: 'https://zhixiao.site:8080/appreciate/new',
        method:'POST',
        data:{
          careHomeName:wx.getStorageSync('beadhouse'),
          userId:wx.getStorageSync('userid'),
          content:this.data.input,
          grade:this.data.value
        },
        header:{
          'Content-Type':'application/json',
          'Cookie':wx.getStorageSync('JSESSIONID')
        },
        success:(res)=>{
          Toast('发布成功')
          console.log(res)
        }
      })
    }else{
      wx.request({
        url: 'https://zhixiao.site:8080/appreciate/new',
        method:'POST',
        data:{
          careHomeName:wx.getStorageSync('beadhouse'),
          userId:wx.getStorageSync('userid'),
          content:this.data.input,
          grade:this.data.value
        },
        header:{
          'Content-Type':'application/json',
        },
        success:(res)=>{
          Toast('发布成功')
          console.log(res)
        }
      })
    }
    
  },
  launch(){
    const that = this
    if(this.data.fileList.length==0){
      this.publish()
      return
    }
    for (var i in this.data.fileList){
      var file = this.data.fileList[i]
      wx.uploadFile({
        url: 'https://zhixiao.site:8080/suggestions/upload', // 仅为示例，非真实的接口地址
        filePath: file.url,
        name: 'file',
        formData: { user: 'test' },
        success(res) {
          console.log(res)
          var url = res.data.slice(32,res.data.length-3)
          console.log(url)
          var pictureList = that.data.pictureList
          pictureList.push(url)
          that.setData({
            pictureList:pictureList
          })
        },
        complete(res){
          //处理异步问题
          if (that.data.pictureList.length === that.data.fileList.length){
            that.publish()
          }
        }
      });
    }
  },
  publish(){
    var url = this.data.pictureList.join(";")
    console.log(url)
    console.log(22)
    wx.request({
      url: 'https://zhixiao.site:8080/suggestions/new',
      method:'POST',
      data:{
        careHomeName:wx.getStorageSync('beadhouse'),
        userId:wx.getStorageSync('userid'),
        content:this.data.input,
        pictureUrl:url
      },
      header:{
        'Content-Type':'application/json',
      },
      success:(res)=>{
        console.log("success")
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})